//方案管理基础API
export const usePlanBaseApi = (Variable, emit,pageID) => {
  const {
    queryPlanList,currectPlan,modifyPlan,$q,queryPlanByShareUrl,mappingProps,dataQueryID
  } = Variable;
  // const { hasAuth, getSubStatus } = usePlanAuthority(Variable);
  // const { getPageUserProfile } = useUserProfile(
  //   UserProfile,
  //   modifyPlan,
  //   pageID,
  // );
  const GetQueryPlanV2=(planId)=>{
    //@ts-ignore
    return TFF.common.ajax.getDataAsync({
      //@ts-ignore
      url: `${Funcs.FasRequestTagAdd(
        //@ts-ignore
        TFF.common.ajax.getServiceUrl('FASR-GetQueryPlanV2'),
      )}`,
      type: 'post',
      data: {
        planId: planId,
      },
    });
  }
  const getDefaultPlan = async () => {
    //@ts-ignore
    const queryIdByWarningUrl =window._getQueryString&& window._getQueryString("PlanId")
    if(queryIdByWarningUrl){
      //列表循环
    
      const defaultPlan =   queryPlanList.value.filter((item) => {
        if (item.ID ==queryIdByWarningUrl) {
          return item;
        }
      });
      //不存在 查询一遍
      if(!defaultPlan||defaultPlan.length==0){
         //@ts-ignore
        queryPlanByShareUrl.value = await GetQueryPlanV2(queryIdByWarningUrl);
        if( queryPlanByShareUrl.value){
          queryPlanByShareUrl.value.PlanRange=3
          queryPlanByShareUrl.value.isReady = true
          queryPlanList.value.forEach(item=>{
            if(item.PlanRange== queryPlanByShareUrl.value.PlanRange&&item.Name== queryPlanByShareUrl.value.Name){
              queryPlanByShareUrl.value.Name=  queryPlanByShareUrl.value.Name+ `(${$q.lang.queryPlan.viewBySub})`
            }
          })
          queryPlanByShareUrl.value.IsAuth=false
          queryPlanByShareUrl.value.IsDefault=false
          queryPlanByShareUrl.value.isfromShare = true
          currectPlan.value = Object.assign({},  queryPlanByShareUrl.value);
          modifyPlan.value = Object.assign({},  queryPlanByShareUrl.value);
          queryPlanList.value.unshift( queryPlanByShareUrl.value)
          return
        }
      }else{
       
          const plans= await GetQueryPlanV2(queryIdByWarningUrl);
     
        // 
        //@ts-ignore
        // defaultPlan.isReady = true
        currectPlan.value = Object.assign({}, plans);
        modifyPlan.value = Object.assign({}, plans);
        return
      }
    }
    let defaultPlanList = queryPlanList.value.filter(
      item => item.IsDefault || item.IsSystem,
    );
    //userProfile 门户方案管理支持记住上次选择的方案
    if(mappingProps.planType=='1'){
      const businessTag = Funcs.GetQueryString('BusinessTag',window.location.href)
      let profiles 
      try {
        profiles = Funcs.GetPageUserProfile({
          profileKeys: {
            Key: `QUERYPLAN_MANAGER_PORTAL`,
            DynamicKey:`${dataQueryID?.value?dataQueryID?.value.replace(/-/ig,'_'):''}${businessTag&&businessTag.replace(/-/ig,'_')} `,
          },
          PageID: pageID.value,
        }) 
        profiles = await profiles
        const configValue = profiles.data[0].ConfigValue
        if(configValue){
          const plan = await GetQueryPlanV2(JSON.parse(configValue).ID)//queryPlanList.value.find(item=>item.ID==JSON.parse(configValue).ID) 
          if(plan){
            defaultPlanList = [plan]
          }
        
        }
      } catch (error) {
        
      }
     
    
      // return 
    }
    let defaultPlan = {};
    if (defaultPlanList.length > 1) {
      defaultPlan = defaultPlanList.find(item => !item.IsSystem);
    } else {
      defaultPlan = defaultPlanList[0];
    }
    if (!defaultPlan) {
      defaultPlan = queryPlanList.value.find(item => item.IsSystem);
    }
    if(queryIdByWarningUrl==defaultPlan.ID){
      defaultPlan== await GetQueryPlanV2(defaultPlan.ID);
    }
    // 
    //@ts-ignore
    // defaultPlan.isReady = true
    currectPlan.value = Object.assign({}, defaultPlan);
    modifyPlan.value = Object.assign({}, defaultPlan);
  };
  const preventDefault = e => {
    if (e.keyCode == 9) {
      document
        .querySelector('.query_plan_target_focuse_dom')?.querySelector('input')?.focus();
      e.stopPropagation();
      e.preventDefault();
      return false;
    }
  };

  return {
    preventDefault,
    getDefaultPlan,
    GetQueryPlanV2
  };
};
